﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Police_Question_ProblemHistory : System.Web.UI.Page
{
    private string ConstDepartment;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConstDepartment = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        this.dptSubject.SelectedIndexChanged += new EventHandler(dptSubject_SelectedIndexChanged);
        if (!IsPostBack)
            Bind();
    }

    void dptSubject_SelectedIndexChanged(object sender, EventArgs e)
    {
        Question2DataContext db = new Question2DataContext();
        var result = from s in db.SecurityProblem2 where s.Sid==new Guid(dptSubject.SelectedValue) select s;
        this.lstProblem.DataSource = result;
        this.lstProblem.DataTextField = "Title";
        this.lstProblem.DataValueField = "ID";
        this.lstProblem.DataBind();
        if (lstProblem.Items.Count > 0)
        {
            this.btnAdd.Enabled = true;
        }
        else
        {
            this.btnAdd.Enabled = false;
        }
    }
    private void Bind()
    {
        Question2DataContext db = new Question2DataContext();
        var result = from v in db.SecuritySubject2 where v.CreateDepartment.Substring(0, 4) == ConstDepartment.Substring(0, 4) select v;
        foreach (SecuritySubject2 s in result)
        {
            ListItem item = new ListItem();
            item.Text = "".PadRight(2 * int.Parse(s.Flag.ToString()), '　') + s.Caption;
            item.Value = s.Id.ToString();

            dptSubject.Items.Add(item);
        }

        if (Request["ID"] != null)
            this.dptSubject.SelectedValue = Request["ID"];

        dptSubject_SelectedIndexChanged(this.dptSubject, new EventArgs());
        
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string strID = string.Empty;
        string strText = string.Empty;
        string ReturnStr = string.Empty;
        string strScr = string.Empty;
        foreach (ListItem item in lstProblem.Items)
        {
            if (item.Selected)
            {
                strID += item.Value+"|";
                strText += item.Text+"|";
            }

        }

        if (strID.Length > 1)
        {
            strID = strID.Remove(strID.Length - 1);
            strText = strText.Remove(strText.Length - 1);
            ReturnStr = strID + "*" + strText + "*" + Cbox.Checked.ToString();
             strScr = "<script language=javascript> window.returnValue='" + ReturnStr + "';  window.close(); </script>";
        }
        else
        {
             strScr = "<script language=javascript>  window.close(); </script>";    
        }
        
    
    
        Page.RegisterClientScriptBlock("Close", strScr);


       
    }


}
